const ResultVo = require('../vo/ResultVo')
const { renderRequestType } = require('../utils/RequestTypeUtil')
const { handleClientAdapter } = require('../db-server/db-Adapter')
const { sign } = require('./util-token')
const HttpStatus = require('../vo/HttpStatus')

function login(req, res, ogisticServiceData) {
  let dataSourceKey = ogisticServiceData.authDataSourceKey
  let omuser = ogisticServiceData.authUserModel
  const client = handleClientAdapter(ogisticServiceData, dataSourceKey)
  const user = new omuser(client)
  let json = req.body
  if (!json.username || !json.password) {
    res.send(
      ResultVo({
        status: 0,
        message: '用户名和密码必须填写!',
        data: null
      })
    )
    return
  }
  user
    .query({
      where: `where username='${json.username}'`
    })
    .then(({ total, data }) => {
      if (total) {
        const { username, password } = data[0]
        let { authPrivateKey, authTokenTime } = ogisticServiceData
        let time = {
          num: 12,
          type: 'hour'
        }
        if (authTokenTime) {
          let num = authTokenTime.replace(/[^-](\d)-|[^\d]/gi, '')
          let type = authTokenTime.replace(num, '')
          time = {
            num: Number(num),
            type
          }
        }
        // wjf to do
        // 改成md5
        if (json.password === password) {
          let token = sign(username, authPrivateKey, time)
          res.send(
            ResultVo({
              status: 0,
              data: {
                token
              }
            })
          )
        } else {
          res.send(
            ResultVo({
              status: HttpStatus.LoginFail.code,
              message: HttpStatus.LoginFail.message,
              data: null
            })
          )
        }
      }
    })
}

function register(req, res, ogisticServiceData) {
  let dataSourceKey = ogisticServiceData.authDataSourceKey
  let omuser = ogisticServiceData.authUserModel
  const client = handleClientAdapter(ogisticServiceData, dataSourceKey)
  const user = new omuser(client)
  let json = req.body
  if (!json.username || !json.password) {
    res.send(
      ResultVo({
        status: 0,
        message: '用户名和密码必须填写!',
        data: null
      })
    )
    return
  }
  user
    .query({
      where: `where username='${json.username}'`
    })
    .then(({ total, data }) => {
      if (!total) {
        // wjf to do
        // 改成md5
        user
          .addItems({
            username: json.username,
            isactive: 1,
            password: json.password,
            realname: 'wujianfei'
          })
          .then((v) => {
            res.send(
              ResultVo({
                status: 0,
                data: v
              })
            )
          })
          .catch((err) => {
            res.send(
              ResultVo({
                status: 1,
                message: err.message || '',
                data: {
                  name: 1111
                }
              })
            )
          })
      } else {
        res.send(
          ResultVo({
            status: 2,
            message: '已经存在',
            data: null
          })
        )
      }
    })
    .catch((err) => {
      res.send(
        ResultVo({
          status: 0,
          message: err.message,
          data: null
        })
      )
    })
}

exports.name = 'AuthController'
exports.login = renderRequestType(login, 'POST', null, null, '登录')
exports.register = renderRequestType(register, 'POST', null, null, '注册')
/**
 * 1.如果不需要token，一定要配置
 * 2.默认false, 全部需要都需要token
 * 3.globalCloseToken是全局token控制
 * 如果有子接口配置，则以子的配置为优先
 */
exports.globalCloseToken = true
exports.childCloseToken = []
